package com.itheima.servlet;

import com.alibaba.fastjson.JSON;
import com.itheima.bean.User;
import com.itheima.service.UserService;
import com.itheima.utils.CookieUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.time.LocalDateTime;

@WebServlet("/userLogin")
public class UserLogin extends HttpServlet {
    //用户登录
    private final UserService userService = new UserService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //取出浏览器里面保存的用户信息
        Cookie[] cookies = req.getCookies();
        int i = 0;
        for (Cookie cookie : cookies) {
            if(cookie.getName().equals("username") || cookie.getName().equals("password")){
                i = i+1;
                break;
            }
        }
        if (i==1) {
            String username = CookieUtil.getCookieByName(cookies, "username").getValue();
            String password = CookieUtil.getCookieByName(cookies, "password").getValue();
            //将信息封装成用户对象
            User user = new User(null, username, password, null);
            String json = JSON.toJSONString(user);
            resp.getWriter().println(json);
        }else {
            resp.getWriter().println("0");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        User user = userService.selectUser(username, password);
        //保证安全将放入的user的密码去掉
        user.setPassword(null);
        if (user == null) {
            //用户为空登录失败使用转发跳回注册页面  并在上方显示登录失败的信息
            req.setAttribute("errorMessage", "用户名或密码错误");
            req.getRequestDispatcher("user-login.html").forward(req, resp);
        }
        //用户不为空登录成功
        String remember = req.getParameter("remember");
        if (remember != null) {
            //如果用户勾选了记住我创建cookie记录用户密码和用户名   有效期为30天
            Cookie cookieUserName = new Cookie("username", username);
            cookieUserName.setMaxAge(60 * 60 * 24 * 30);
            Cookie cookiePassword = new Cookie("password", password);
            cookiePassword.setMaxAge(60 * 60 * 24 * 30);
            resp.addCookie(cookieUserName);
            resp.addCookie(cookiePassword);
        }
        //将用户信息放入会话域
        HttpSession session = req.getSession();
        session.setAttribute("user",user);
        //创建一个cookie记录用户的登录时间有效期为30天
        //先拿出上次存的时间值
        Cookie[] cookies = req.getCookies();
        Cookie beforeTime = CookieUtil.getCookieByName(cookies, "time");
        String beforeTimeValue = "这是第一次登录!!";
        if(beforeTime!=null){
            beforeTimeValue = beforeTime.getValue();
        }
        req.getSession().setAttribute("time",beforeTimeValue);
        //将这次的时间记录下来
        String nowTime = LocalDateTime.now().toString();
        Cookie time = new Cookie("time",nowTime);
        time.setMaxAge(60*60*24*30);
        resp.addCookie(time);
        //登录成功跳转到投票页面
        resp.sendRedirect("home.html");
    }
}
